package atguigu.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

public class StreamDemo {

	List<Person> personList = new ArrayList<Person>();

	public StreamDemo() {
		personList = new ArrayList<Person>();
		personList.add(new Person("Tom", 8900, "male", "New York"));
		personList.add(new Person("Jack", 7000, "male", "Washington"));
		personList.add(new Person("Lily", 7800, "female", "Washington"));
		personList.add(new Person("Anni", 8200, "female", "New York"));
		personList.add(new Person("Owen", 9500, "male", "New York"));
		personList.add(new Person("Alisa", 7900, "female", "New York"));
		personList.add(new Person("Alisa", 7800, "female", "New York"));
		personList.add(new Person("Alisa", 7600, "female", "New York"));
	}

	public static void main(String[] args) {
		StreamDemo demo = new StreamDemo();
//		demo.test12();

		demo.test13();

//        demo.test1();
//        demo.test2();
//        demo.test3();
//		  demo.test4();
//		  demo.test5();
//		demo.test6();
//		demo.test7();
//		demo.test8();
//		demo.test9();
//		demo.test10();
//		demo.test11();

	}

	private void test13(){
		List<Person> alisa = personList.stream()
				.filter(e -> e.getName().equals("Alisa")).sorted(new Comparator<Person>() {
					@Override
					public int compare(Person o1, Person o2) {
						return o1.getSalary() - o2.getSalary();
					}
				})
				.collect(Collectors.toList());
		for (Person person : alisa) {
			System.out.println(person);
		}
	}

	private void test12(){
		String s = personList.stream().map(e -> e.getName()).filter(e -> e.contains("Owen")).collect(Collectors.toList()).get(0);
		System.out.println(s);
	}

	private void test1() {
		List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
		// 遍历输出符合条件的元素
		list.stream() //
				.filter(x -> x > 6) //
				.forEach(System.out::println); //
	}

	public void test2() {
		List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

		// 匹配第一个
		Optional<Integer> findFirst = list.stream() //
				.filter(x -> x > 6) //
				.findFirst(); //

		// 匹配任意（适用于并行流）
		Optional<Integer> findAny = list.parallelStream() //
				.filter(x -> x > 6) //
				.findAny(); //

		// 是否包含符合特定条件的元素
		boolean anyMatch = list.stream() //
				.anyMatch(x -> x < 6); //

		System.out.println("匹配第一个值：" + findFirst.get());
		System.out.println("匹配任意一个值：" + findAny.get());
		System.out.println("是否存在大于6的值：" + anyMatch);
	}

	public void test3() {
		List<Integer> list = Arrays.asList(6, 7, 3, 8, 1, 2, 9);
		Stream<Integer> stream = list.stream();
		stream.filter(x -> x > 7) //
				.forEach(System.out::println); //
	}

	public void test4() {
		List<Person> personList = new ArrayList<Person>();
		personList.add(new Person("Tom", 8900, 23, "male", "New York"));
		personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
		personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
		personList.add(new Person("Anni", 8200, 24, "female", "New York"));
		personList.add(new Person("Owen", 9500, 25, "male", "New York"));
		personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

		List<String> fiterList = personList.stream() //
				.filter(x -> x.getSalary() > 8000) //
				.map(Person::getName) //
				.collect(Collectors.toList()); //

		System.out.print("高于8000的员工姓名：" + fiterList);
	}

	public void test5() {
		List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
//        Optional<String> max = list.stream().max(Comparator.comparing(String::length));
//        Optional<String> max = list.stream().max(Comparator.comparing(String::toString));
		Optional<String> max = list.stream().max(String::compareTo);
		System.out.println("最长的字符串：" + max.get());
	}

	@Test
	public void test6() {
		List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);

		// 自然排序
		Optional<Integer> max = list.stream().max(Integer::compareTo);

		// 自定义排序
		Optional<Integer> max2 = list.stream().max(new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o1.compareTo(o2);
			}
		});

		System.out.println("自然排序的最大值：" + max.get());
		System.out.println("自定义排序的最大值：" + max2.get());
	}

	private void test7() {
		List<Person> personList = new ArrayList<Person>();
		personList.add(new Person("Tom", 8900, 23, "male", "New York"));
		personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
		personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
		personList.add(new Person("Anni", 8200, 24, "female", "New York"));
		personList.add(new Person("Owen", 9500, 25, "male", "New York"));
		personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

		Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getSalary));
		System.out.println("员工工资最大值：" + max.get().getSalary());
	}

	private void test8() {
		List<Integer> list = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
		long count = list.stream().filter(x -> x > 6).count();
		System.out.println("list中大于6的元素个数：" + count);
	}

	private void test9() {
		
		List<Person> personList = new ArrayList<Person>();
		
		personList.add(new Person("Tom", 8900, 23, "male", "New York"));
		personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
		personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
		personList.add(new Person("Anni", 8200, 24, "female", "New York"));
		personList.add(new Person("Owen", 9500, 25, "male", "New York"));
		personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

		// 不改变原来员工集合的方式
		List<Person> personListNew = personList.stream().map(person -> {
			Person personNew = new Person(person.getName(), 0, 0, null, null);
			personNew.setSalary(person.getSalary() + 10000);
			return personNew;
		}).collect(Collectors.toList());
		
		System.out.println("一次改动前：" + personList.get(0).getName() + "-->" + personList.get(0).getSalary());
		System.out.println("一次改动后：" + personListNew.get(0).getName() + "-->" + personListNew.get(0).getSalary());

		// 改变原来员工集合的方式
		List<Person> personListNew2 = personList.stream().map(person -> {
			person.setSalary(person.getSalary() + 10000);
			return person;
		}).collect(Collectors.toList());
		
		System.out.println("二次改动前：" + personList.get(0).getName() + "-->" + personList.get(0).getSalary());
		System.out.println("二次改动后：" + personListNew2.get(0).getName() + "-->" + personListNew2.get(0).getSalary());
	}
	
	private void test10() {
		List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
        List<String> listNew = list.stream().flatMap(s -> {
            // 将每个元素转换成一个stream
            String[] split = s.split(",");
            Stream<String> s2 = Arrays.stream(split);
            return s2;
        }).collect(Collectors.toList());

        System.out.println("处理前的集合：" + list);
        System.out.println("处理后的集合：" + listNew);
	}

	private void test11() {
		
		List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4);
		
        // 求和方式1
        Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
        
        // 求和方式2
        Optional<Integer> sum2 = list.stream().reduce(Integer::sum);
        
        // 求和方式3
        Integer sum3 = list.stream().reduce(0, Integer::sum);

        // 求乘积
        Optional<Integer> product = list.stream().reduce((x, y) -> x * y);

        // 求最大值方式1
        Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y);
        
        // 求最大值写法2
        Integer max2 = list.stream().reduce(1, Integer::max);
        
        

        System.out.println("list求和：" + sum.get() + "," + sum2.get() + "," + sum3);
        System.out.println("list求积：" + product.get());
        System.out.println("list求和：" + max.get() + "," + max2);
	}
}
